package com.yanqu.road.server.cmd.activity.lottery;

import com.yanqu.road.entity.activity.lottery.ActivityLotteryNotice;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityLotteryProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.lottery.ActivityLotteryActivity;
import com.yanqu.road.server.manager.activity.lottery.CrossActivityLotteryMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = CrossProtocol.C_ACTIVITY_LOTTERY_GET_NOTICE, desc = "公告")
public class CrossActivityLotteryGetNoticeCmd extends CrossCommand {

    private int activityId;
    private long userId;

    private List<ActivityLotteryNotice> noticeList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ActivityLotteryProto.CrossGetActivityLotteryNoticeReqMsg reqMsg = ActivityLotteryProto.CrossGetActivityLotteryNoticeReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ActivityLotteryActivity activity = CrossActivityLotteryMgr.getLotteryActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_GOURD_CHILD_ACTIVITY_NO_OPEN;
            return;
        }

        noticeList = activity.getNoticeData();
    }

    @Override
    public void sendData() {
        ActivityLotteryProto.GetActivityLotteryNoticeRespMsg.Builder builder = ActivityLotteryProto.GetActivityLotteryNoticeRespMsg.newBuilder();
        builder.setRet(ret);
        builder.setActivityId(activityId);
        for (int i = noticeList.size() - 1; i >= 0; i--) {
            ActivityLotteryNotice notice = noticeList.get(i);
            builder.addNoticeList(CrossActivityLotteryMgr.parseNoticeMsg(notice));
        }
        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(ClientProtocol.U_ACTIVITY_LOTTERY_DRAW_NOTICE, builder));
    }
}
